from datetime import datetime
from json import dumps

from flask import Blueprint, jsonify, Flask, current_app, make_response, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from flask_restx import Resource, Api, reqparse

from exts import db
from models import order
from models.power import Power
from utils.http import success_api, fail_api
from utils.tree import Tree

power_bp = Blueprint("power",__name__,url_prefix="/power")

api = Api(power_bp)

@power_bp.get("/list")
@jwt_required(optional=False)
def getList():
    query = Power.query
    data = query.all()
    datas = [item.to_dict() for item in data]
    print(datas)
    res = Tree(data=datas).build_tree()
    return success_api("成功", res)
@power_bp.get("/all")
@jwt_required(optional=False)
def getAll():
    data = Power.query.all()
    return success_api("成功", data)
@power_bp.post("/")
@jwt_required(optional=False)
def createPower():
    parser = reqparse.RequestParser()
    # 2.利用解析器对象添加 需要验证的参数
    parser.add_argument('name', type=str, help='名称验证失败！', required=True, trim=True)
    parser.add_argument('parent_id', type=str, help='备注验证失败！',default=0,trim=True)
    parser.add_argument('url', type=str, help='路由验证失败！',required=True, trim=True)
    parser.add_argument('type', type=int, help='详情验证失败！',default=0, trim=True)
    parser.add_argument('enable', type=str, help='状态验证失败！',default=0, trim=True)
    parser.add_argument('open_type', type=str, help='打开方式验证失败！', trim=True)
    parser.add_argument('sort', type=int, help='打开方式验证失败！', trim=True)
    try:
        args = parser.parse_args()
    except Exception as e:
        return {"message": e.data['errors'], "code": 0}
    try:
        power = Power.to_model(**args)
        #power.enable = 1 if power.enable == True else 0
        db.session.add(power)
        db.session.commit()
        return success_api("创建成功", power.id)
    except:
        return fail_api("创建失败")
@power_bp.patch("/<int:id>")
@jwt_required(optional=False)
def updatePower(id):
    parser = reqparse.RequestParser()
    # 2.利用解析器对象添加 需要验证的参数
    parser.add_argument('name', type=str, help='名称验证失败！', required=True, trim=True)
    parser.add_argument('parent_id', type=str, help='备注验证失败！',default=0,trim=True)
    parser.add_argument('url', type=str, help='路由验证失败！',required=True, trim=True)
    parser.add_argument('type', type=int, help='详情验证失败！',default=0, trim=True)
    parser.add_argument('enable', type=str, help='状态验证失败！',default=0, trim=True)
    parser.add_argument('open_type', type=str, help='打开方式验证失败！', trim=True)
    parser.add_argument('sort', type=int, help='打开方式验证失败！', trim=True)
    try:
        args = parser.parse_args()
    except Exception as e:
        return {"message": e.data['errors'], "code": 0}
    try:
        #power = power.to_model(**args)
        #power.enable = 1 if power.enable == True else 0
        Power.query.filter_by(id=id).update(args)
        db.session.commit()
        return success_api("修改成功")
    except Exception as e:
        print(e)
        return fail_api("修改失败")
@power_bp.delete("/<int:id>")
@jwt_required(optional=False)
def deletePower(id):
    power = Power.query.get(id)
    parent =  Power.query.filter_by(parent_id=id).first()
    if parent:
        return fail_api("无法删除父类分类")
    try:
        db.session.delete(power)
        db.session.commit()
        return success_api("删除成功")
    except:
        return fail_api("删除失败")
@power_bp.patch("/enable/<int:id>")
@jwt_required(optional=False)
def updateEnable(id):
    power = Power.query.get(id)
    try:
        power.enable = 0 if power.enable ==True else 1
        db.session.commit()
        return success_api("获取成功",power)
    except:
        return fail_api("获取失败")
@power_bp.get("/<int:id>")
@jwt_required(optional=False)
def getShow(id):
    data = Power.query.get(id)
    return success_api("成功", data)